package com.LachesismStorm.lachesism.core.AiClient;

/**
 * @Author lachesism
 * @Date 2025-02-23 02:53
 **/
import okhttp3.*;
import java.io.IOException;

public class ChatGPTClient {

    private static final String API_KEY = "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"; // 替换为你的API密钥
    private static final String URL = "https://api.openai.com/v1/chat/completions";

    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient();

        // 创建请求体
        String jsonBody = "{"
                + "\"model\": \"gpt-3.5-turbo\","
                + "\"messages\": ["
                + "  {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},"
                + "  {\"role\": \"user\", \"content\": \"Hello, how are you?\"}"
                + "]}";

        RequestBody body = RequestBody.create(jsonBody, MediaType.parse("application/json"));

        // 构造请求
        Request request = new Request.Builder()
                .url(URL)
                .post(body)
                .addHeader("Authorization", "Bearer " + API_KEY)
                .addHeader("Content-Type", "application/json")
                .build();

        // 发送请求
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String responseBody = response.body().string();
                System.out.println("Response: " + responseBody);
            } else {
                System.out.println("Request failed: " + response.message());
            }
        }
    }
}